package com.liuwei.spring.cloud.configuration;

/**
 * @author : evan.liu
 * @version V1.0
 * @Description: TODO
 * @date Date : 2022年05月01日 3:31 上午
 */

import com.liuwei.spring.cloud.biz.support.GlobalExceptionUtils;
import com.liuwei.spring.cloud.framework.dto.Result;
import com.liuwei.spring.cloud.common.exception.ErrorCode;
import com.liuwei.spring.cloud.common.exception.IBizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ResponseBody
    @ExceptionHandler
    public Result exceptionHandler(Exception e) throws Exception {
        log.error("GlobalException|error:{}",e.getMessage(),e);
        try {
            GlobalExceptionUtils.handleGlobalException(e);
        } catch (Exception exception) {
            log.error(e.getMessage());
        }
        if(e instanceof IBizException){
            IBizException ex = (IBizException) e;
            Result ret = Result.fail(ex.getCode(),ex.getMessage());
            return ret;
        }else{
            // 正常返回
            Result ret = Result.fail(ErrorCode.SYSTEM_ERROR,e.getMessage());
            return ret;
        }
    }
}

